﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VROpen
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            // 加载图标、标题、背景等相关配置
            load_config();
        }

        private void load_config()
        {
            // 加载页面背景主题
            String bgPath = System.Configuration.ConfigurationManager.AppSettings["background"];
            if (File.Exists(bgPath))
            {
                ImageBrush brush = new ImageBrush();
                brush.Stretch = Stretch.Fill;
                brush.ImageSource = new BitmapImage(new Uri(bgPath, UriKind.Relative));
                this.Background = brush;
            } else {
                MessageBox.Show("未找到背景图片", "运行错误", MessageBoxButton.OK);
            }

            // 加载图标等相关配置

            Image[] imgArr = { img1, img2, img3, img4, img5, img6, img7, img8 };
            Button[] btnArr = { btn11, btn12, btn21, btn22, btn31, btn32, btn41, btn42, btn51, btn52, btn61, btn62, btn71, btn72, btn81, btn82 };
            TextBlock[] textArr = { text1, text2, text3, text4, text5, text6, text7, text8 };
            for(int i=0; i<8; i++){
                String imgPath = System.Configuration.ConfigurationManager.AppSettings["app_" + (i + 1) + "_img"];
                String title = System.Configuration.ConfigurationManager.AppSettings["app_" + (i + 1) + "_title"];
                //String imgPath = System.Configuration.ConfigurationManager.AppSettings["app_1_img"];
                if (File.Exists(imgPath))
                {
                    string path = System.Environment.CurrentDirectory + "/" + imgPath;//获取图片绝对路径
                    BitmapImage image = new BitmapImage(new Uri(path, UriKind.Absolute));//打开图片
                    imgArr[i].Source = image;//将控件和图片绑定，logo为Image控件名称
                    textArr[i].Text = title;
                }
                else
                {
                    imgArr[i].Source = null;
                    textArr[i].Visibility = System.Windows.Visibility.Hidden;
                    btnArr[i*2].Visibility = System.Windows.Visibility.Hidden;
                    btnArr[i*2 + 1].Visibility = System.Windows.Visibility.Hidden;
                }
            }
            
            
            
            //Uri u = new Uri("Images/m0.png"，UriKind.Relative);
            //ima.UriSource = u;
            //PictureImage.Source = img; 
            
        }

        private void btn1_Open(object sender, RoutedEventArgs e)
        {
            open_exe(1);
        }
        private void btn2_Open(object sender, RoutedEventArgs e)
        {
            open_exe(2);
        }
        private void btn3_Open(object sender, RoutedEventArgs e)
        {
            open_exe(3);
        }
        private void btn4_Open(object sender, RoutedEventArgs e)
        {
            open_exe(4);
        }
        private void btn5_Open(object sender, RoutedEventArgs e)
        {
            open_exe(5);
        }
        private void btn6_Open(object sender, RoutedEventArgs e)
        {
            open_exe(6);
        }
        private void btn7_Open(object sender, RoutedEventArgs e)
        {
            open_exe(7);
        }
        private void btn8_Open(object sender, RoutedEventArgs e)
        {
            open_exe(8);
            //close_exe("PhotoShop");
        }

        private void btn1_Close(object sender, RoutedEventArgs e)
        {
            close_exe(1);
        }
        private void btn2_Close(object sender, RoutedEventArgs e)
        {
            close_exe(2);
        }
        private void btn3_Close(object sender, RoutedEventArgs e)
        {
            close_exe(3);
        }
        private void btn4_Close(object sender, RoutedEventArgs e)
        {
            close_exe(4);
        }
        private void btn5_Close(object sender, RoutedEventArgs e)
        {
            close_exe(5);
        }
        private void btn6_Close(object sender, RoutedEventArgs e)
        {
            close_exe(6);
        }
        private void btn7_Close(object sender, RoutedEventArgs e)
        {
            close_exe(7);
        }
        private void btn8_Close(object sender, RoutedEventArgs e)
        {
            close_exe(8);
            //close_exe("PhotoShop");
        }

        private void open_exe(int p)
        {
            //MessageBox.Show("这里是消息内容", "这是标题" + p, MessageBoxButton.YesNo);
            string exe_path = System.Configuration.ConfigurationManager.AppSettings["app_" + p + "_path"];
            Process process = new Process();
            //定义消息框             
            string caption = "应用控制" + p;
            string messageBoxText = "即将开启体验\n" + exe_path;
            MessageBoxButton button = MessageBoxButton.OK;
            MessageBoxImage icon = MessageBoxImage.Information;
            //显示消息框              
            MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, icon);
            //处理消息框信息              
            switch (result)
            {
                case MessageBoxResult.OK:
                    try
                    {
                        process.StartInfo.UseShellExecute = true;
                        process.StartInfo.FileName = exe_path;
                        process.Start();
                        //process.WaitForExit();
                        //process.Close();

                        //InitializeComponent();
                        //System.Diagnostics.Process.Start(@exe_path);　　　　//调用该命令，打开exe_path程序
                    }
                    catch (Exception)
                    {
                    }
                    break;
                case MessageBoxResult.No:
                    break;
                case MessageBoxResult.Cancel:
                    // ...                     
                    break;
            }
        }

        private void close_exe(int p)
        {
            string exe_name = System.Configuration.ConfigurationManager.AppSettings["app_" + p + "_name"];
            //定义消息框             
            string caption = "应用控制" + p;
            string messageBoxText = "即将关闭 " + exe_name;
            MessageBoxButton button = MessageBoxButton.OK;
            MessageBoxImage icon = MessageBoxImage.Information;
            //显示消息框              
            MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, icon);
            //处理消息框信息              
            switch (result)
            {
                case MessageBoxResult.OK:
                    close_exe(exe_name);
                    break;
                case MessageBoxResult.No:
                    break;
                case MessageBoxResult.Cancel:
                    // ...                     
                    break;
            }
        }

        private void close_exe(String exe_name)
        {
            Process[] myProgress;
            myProgress = Process.GetProcesses();　　　　　　　　　　//获取当前启动的所有进程
            foreach (Process p in myProgress)　　　　　　　　　　　　//关闭当前启动的Excel进程
            {
                if (p.ProcessName == exe_name)　　　　　　　　　　//通过进程名来寻找
                {
                    p.Kill();
                    return;
                }
            }
        }
    }
}
